Imports System.Data
Imports System.Data.SqlClient

Public Class UsersList

    Dim WithEvents oBinding As Binding

    Private Sub UsersList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        RefreshGrid()

    End Sub

    Private Sub ClearLabelText()
        Me.lblBirthDay.Text = ""
        Me.lblEmail.Text = ""
        Me.lblMobileNo.Text = ""
        Me.lblName.Text = ""
        Me.lblPosition.Text = ""
        Me.chkStatus.EditValue = 0
    End Sub


    Private Sub RefreshGrid()
        Dim str As String = "SELECT Users.*, Position.PositionName, UserLevels.UserLevelsName " & _
        "FROM Users LEFT OUTER JOIN " & _
        "UserLevels ON Users.AccessLevel = UserLevels.ID LEFT OUTER JOIN " & _
        "Position ON Users.Position = Position.ID " & _
        "WHERE Status = 1"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "Users")
        Me.GridControl1.DataSource = ds.Tables("Users")

        Me.chkStatus.DataBindings.Clear()
        chkStatus.DataBindings.Add(New Binding("Checked", ds.Tables("Users"), "Status", True))

        Me.lblID.DataBindings.Clear()
        lblID.DataBindings.Add(New Binding("Text", ds.Tables("Users"), "ID", True))

        Me.lblName.DataBindings.Clear()
        lblName.DataBindings.Add(New Binding("Text", ds.Tables("Users"), "UserName", True))

        Me.lblPosition.DataBindings.Clear()
        lblPosition.DataBindings.Add(New Binding("Text", ds.Tables("Users"), "PositionName", True))

        Me.lblEmail.DataBindings.Clear()
        lblEmail.DataBindings.Add(New Binding("Text", ds.Tables("Users"), "Email", True))

        Me.lblMobileNo.DataBindings.Clear()
        lblMobileNo.DataBindings.Add(New Binding("Text", ds.Tables("Users"), "MobileNO", True))

        Me.lblBirthDay.DataBindings.Clear()
        oBinding = New Binding("Text", ds.Tables("Users"), "BirthDay", True)
        lblBirthDay.DataBindings.Add(oBinding)

        Me.PictureEdit1.DataBindings.Clear()
        PictureEdit1.DataBindings.Add(New Binding("Image", ds.Tables("Users"), "Image", True))
    End Sub


    Private Sub oBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles oBinding.Format
        e.Value = Format(e.Value, "dd-MM")
    End Sub



    Private Sub chkStatus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkStatus.CheckedChanged
        If Me.chkStatus.Checked = True Then
            Me.chkStatus.ForeColor = Color.Green
        Else
            Me.chkStatus.ForeColor = Color.Gray
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim frm As New UserDetailvb
        frm.Action = "ADD"
        frm.ShowDialog()
        RefreshGrid()



    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.lblID.Text <> "" Then
            Dim frm As New UserDetailvb
            frm.Action = "EDIT"
            frm.ID = lblID.Text
            frm.ShowDialog()
            RefreshGrid()
        End If


    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If MsgBox("Are You Sure Want to Delete ?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If

        If GridView1.RowCount = 1 Then
            MsgBox("The User Should Be At Least Remain 1", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
    
        Dim str As String
        str = "UPDATE Users SET Status = 0 WHERE ID = " & Me.lblID.Text
        Dim cmd As New SqlCommand(str, gCnnPCE)
        If cmd.ExecuteNonQuery > 0 Then
            MsgBox("Delete Successfully", MsgBoxStyle.Information)
            RefreshGrid()
        Else
            MsgBox("Fail To Delete", MsgBoxStyle.Exclamation)
        End If

        'check many many table
        'Dim str As String
        'Dim cmd As New SqlCommand("", gCnnPCE)
        'ItemsRecords
        ' str = "SELECT COUNT(*) FROM "
        'Users
        'StockRecords
        'Records
        'TopUpRecords
    End Sub
End Class